package com.zhihuitong.meeting.dao;

import android.content.Context;


import org.litepal.LitePal;
import org.litepal.crud.async.FindMultiExecutor;
import org.litepal.crud.callback.FindMultiCallback;

import java.util.List;

/**
 * 作者： dasd
 * 时间 2018/6/2.
 * 文件描述：
 * 版本号：V 1.0
 **/

public class SQLiteUtils {
    public static void init(Context context) {
        LitePal.initialize(context);
    }

    /**
     * 异步查询所有会议
     *
     * @param callback
     */
    public static void findAllMsgAsync(FindMultiCallback callback) {
        FindMultiExecutor<LocalMeetingBean> allAsync = LitePal.order("createTime DESC").findAsync(LocalMeetingBean.class);
        allAsync.listen(callback);
    }

    /**
     * 同步查询所有会议
     */
    public static List<LocalMeetingBean> findAllMsg(String conferenceID, String createTime, String length) {
        return LitePal.order("createTime DESC")
                .where("conferenceID = ? and createTime = ? and length = ? ", conferenceID, createTime, length)
                .find(LocalMeetingBean.class);
    }

    /**
     * 同步分页查询
     *
     * @param offset 偏移量
     * @return
     */
    public static List<LocalMeetingBean> findAllMsgOffset(int offset) {
        List<LocalMeetingBean> newsList = LitePal
                .order("createTime desc").limit(20).offset(offset)
                .find(LocalMeetingBean.class);
        return newsList;
    }

    /**
     * 异步查询分页所有会议
     * * @param userId 用户id
     * * @param offset 偏移量
     *
     * @param callback
     */
    public static void findAllMsgAsync(int offset, FindMultiCallback callback) {
        FindMultiExecutor<LocalMeetingBean> allAsync = LitePal
                .order("createTime DESC")
                .limit(20).offset(offset)
                .findAsync(LocalMeetingBean.class);
        allAsync.listen(callback);
    }


    /**
     * 删除所有数据
     */
    public static boolean deletAll() {
        try {
            LitePal.deleteAll(LocalMeetingBean.class);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 删除所有数据
     */
    public static boolean deletMeetById(long id) {
        try {
            int delete = LitePal.delete(LocalMeetingBean.class, id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

}
